package com.qdxwx.manage.controller.role;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.ManagerService;
import com.qdxwx.data.service.manager.RoleService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.manager.ManagerRole;
import com.qdxwx.models.manager.Role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/role/auth")
public class RoleAuthController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ManagerService managerService;


    /**
     * 分配用户
     */
    @Permissions("role:edit")
    @GetMapping("/list.html/{roleId}")
    public String authManager(@PathVariable("roleId") int roleId, ModelMap mmap) {
        if (roleId < 1) return error404;
        Role role = roleService.get(roleId);
        if (role == null) return error500;
        mmap.put("role", role);
        return "role/authUser";
    }

    /**
     * 查询已分配用户角色列表
     */
    @Permissions("role:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo allocatedList(HttpServletRequest request) {
        String loginName = request.getParameter("loginName");
        String mobile = request.getParameter("mobile");
        Manager manager = new Manager();
        manager.setLoginName(TKString.queryKey(loginName));
        manager.setMobile(TKString.isMobile(mobile)?mobile:null);
        manager.setRoleId(TKConvert.toInteger(request.getParameter("roleId")));
        List<Manager> list = managerService.getAllocatedList(manager, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(managerService.countAllocated(manager));
        return rspData;
    }

    /**
     * 取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult cancelAuthManager(HttpServletRequest request) {
        int roleId = TKConvert.toInteger(request.getParameter("roleId"));
        int managerId = TKConvert.toInteger(request.getParameter("managerId"));
        if(roleId < 1 || managerId < 1) return error("参数异常");
        ManagerRole managerRole = new ManagerRole();
        managerRole.setManagerId(managerId);
        managerRole.setRoleId(roleId);
        return toAjax(roleService.deleteAuthManager(managerRole));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/removeAll.json")
    @ResponseBody
    public AjaxResult cancelAuthUserAll(HttpServletRequest request) {
        int roleId = TKConvert.toInteger(request.getParameter("roleId"));
        Integer[] userIds = TKConvert.toIntArray(request.getParameter("userIds"));
        if(roleId < 1) return error("缺少id参数");
        if(userIds.length < 1) return error("至少选择一条记录");
        return toAjax(roleService.deleteAuthManagers(roleId, userIds));
    }

    /**
     * 选择用户
     */
    @GetMapping("/selectUser/{roleId}")
    public String selectManager(@PathVariable("roleId") int roleId, ModelMap mmap) {
        if(roleId < 1) return error500;
        Role role = roleService.get(roleId);
        if(role == null) return error500;
        mmap.put("role", role);
        return "role/selectUser";
    }

    /**
     * 查询未分配用户角色列表
     */
    @Permissions("role:list")
    @PostMapping("/selectUser/list.json")
    @ResponseBody
    public TableDataInfo unallocatedList(HttpServletRequest request) {
        String searchKey = request.getParameter("searchKey");
        Manager manager = new Manager();
        if (TKString.isNotEmpty(searchKey)){
            if(TKString.isMobile(searchKey)){
                manager.setMobile(searchKey);
            }else {
                manager.setManagerName(searchKey);
            }
        }
        manager.setRoleId(TKConvert.toInteger(request.getParameter("roleId")));
        List<Manager> list = managerService.getUnallocatedList(manager, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(managerService.countUnallocated(manager));
        return rspData;
    }

    /**
     * 批量选择用户授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult selectAuthUserAll(HttpServletRequest request) {
        int roleId = TKConvert.toInteger(request.getParameter("roleId"));
        if(roleId < 1) return error("缺少id参数");
        Integer[] userIds = TKConvert.toIntArray(request.getParameter("userIds"));
        if(userIds.length < 1) return error("至少选择一条数据");
        return toAjax(roleService.insertAuthManagers(roleId, userIds));
    }
}